package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShopCartMapper;
import com.sky.service.ShopCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class ShopCartServiceImpl implements ShopCartService {
    @Autowired
    private ShopCartMapper shopCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public List<ShoppingCart> list() {
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(BaseContext.getCurrentId())
                .build();
        List<ShoppingCart> list = shopCartMapper.list(shoppingCart);
        return list;
    }

    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> list = shopCartMapper.list(shoppingCart);
        if(list!=null && list.size()>0){
            ShoppingCart shoppingCart_tmp = list.get(0);
            shoppingCart_tmp.setNumber(shoppingCart_tmp.getNumber()+1);
            shopCartMapper.update(shoppingCart_tmp);
        }else{
            shoppingCart.setNumber(1);
            if(shoppingCart.getDishId()!=null){
                Dish dish=dishMapper.getById(shoppingCart.getDishId());
                BeanUtils.copyProperties(dish,shoppingCart);
                shoppingCart.setAmount(dish.getPrice());
            }else if(shoppingCart.getSetmealId()!=null){
                Setmeal setmeal=setmealMapper.getById(shoppingCart.getSetmealId());
                BeanUtils.copyProperties(setmeal,shoppingCart);
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shopCartMapper.add(shoppingCart);
        }





    }
    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> list = shopCartMapper.list(shoppingCart);
        if(list!=null && list.size()>0){
            ShoppingCart shoppingCart_tmp = list.get(0);
            if(shoppingCart_tmp.getNumber()==1){
                shopCartMapper.delete(shoppingCart_tmp);
                return;
            }else if(shoppingCart_tmp.getNumber()>1){
                shoppingCart_tmp.setNumber(shoppingCart_tmp.getNumber()-1);
                shopCartMapper.update(shoppingCart_tmp);
            }

        }

    }

    @Override
    public void clean() {
        shopCartMapper.clean(BaseContext.getCurrentId());
    }
}
